package com.company.service.impl;


import com.company.dao.CheckGroupDao;
import com.company.entity.PageResult;
import com.company.pojo.CheckGroup;
import com.company.pojo.CheckItem;
import com.company.service.CheckGroupService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.dubbo.config.annotation.DubboService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fu xiao liu
 * @date 2021-08-05 15:16
 */

@DubboService(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;


    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page);
    }

    @Override
    public void addCheckGroup(CheckGroup checkGroup, Integer[] checkItemIds) {

        //增加主表
        checkGroupDao.addCheckGroup(checkGroup);
        Integer checkGroupId = checkGroup.getId();


        //判断是否需要增加从表
        if(checkItemIds!=null && checkItemIds.length > 0){
            for (Integer checkItemId : checkItemIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkItemId",checkItemId);
                checkGroupDao.addCheckGroupItemIds(map);
            }
        }
    }

    @Override
    public List<CheckItem> findAllFromCheckItem() {

        return  checkGroupDao.findAllFromCheckItem();
    }

    @Override
    public void delete(Integer id) {
        //删除外键 Item  and  Group 关联
        checkGroupDao.deleteItemGroup(id);
        //删除外键 Group and SetMeal 关联
        checkGroupDao.deleteGroupSetMeal(id);
        //删除主表
        checkGroupDao.deleteGroup(id);
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkItemIds) {
        //修改主表
        checkGroupDao.editCheckGroup(checkGroup);
        Integer checkGroupId = checkGroup.getId();

        //删除从表
        checkGroupDao.deleteItemGroup(checkGroupId);

        //增加从表
        if(checkItemIds!=null && checkItemIds.length > 0){
            for (Integer checkItemId : checkItemIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkItemId",checkItemId);
                checkGroupDao.addCheckGroupItemIds(map);
            }
        }
    }
}
